package models;

import java.util.Date;
import java.util.List;



import play.Logger;
import play.data.validation.Required;
import siena.Filter;
import siena.Id;
import siena.Model;
import siena.Query;
import siena.core.lifecycle.PostInsert;
import siena.core.lifecycle.PreInsert;
import siena.core.lifecycle.PreUpdate;


public class Focus extends Model {
	/** The focus id */
	@Id public Long id;
	
	/** The focus title */
	@Required public String title;

	/** The focus description */
	@Required public String description;
	
	/** The focus owner */
	@Required public User owner;
	
	/** The list of the Focus' parents */
	public List<Focus> parents;
	
	public Date created;
	
	public Date updated;
	
	/** The focus owner */
	@Filter("focus")
	public Query<InformationRequest> informationRequest;
	
	/** The focus owner */
	@Filter("focus")
	public Query<IdeaRequest> ideasRequest;
	
	public static Query<Focus> all() {
		return all(Focus.class);
	}
	
	public static Query<Focus> findByOwner(User user) {
		return all().filter("owner", user);
	}
	
	public static List<Focus> getByOwner(User user) {
		return findByOwner(user).fetch();
	}
	
	public static Query<Focus> findById(Long id, Query<Focus> query) {
		if(query == null) query = all();
		return query.filter("id", id);
	}
	
	public static Focus getById(Long id, Query<Focus> query) {
		return findById(id, query).get();
	}
	
	@PreUpdate
	public void editedDate() {
		Logger.info("Setting edited date of the focus: " + id);
	}
	
	@PostInsert
	@PreInsert
	public void createdDate() {
		Logger.info("Setting created date of the focus: ");
	}
	
	@Override
	public void save() {
		if(this.id == null) this.created = new Date();
		else this.updated = new Date();
		super.save();
	}
}
